package com.ruoyi.project.modules.parameter.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.mapper.InterfaceParamsFieldRelationMapper;
import com.ruoyi.project.modules.table.domain.FieldInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.parameter.mapper.ParamsMapper;
import com.ruoyi.project.modules.parameter.domain.Params;
import com.ruoyi.project.modules.parameter.service.IParamsService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 参数数据集Service业务层处理
 *
 * @author smallrain
 * @date 2021-10-30
 */
@Service
public class ParamsServiceImpl implements IParamsService {
    @Resource
    private ParamsMapper paramsMapper;

    @Resource
    private InterfaceParamsFieldRelationMapper interfaceParamsFieldRelationMapper;

    /**
     * 查询参数数据集
     *
     * @param id 参数数据集主键
     * @return 参数数据集
     */
    @Override
    public Params selectParamsById(Long id) {
        return paramsMapper.selectParamsById(id);
    }

    /**
     * 查询参数数据集列表
     *
     * @param params 参数数据集
     * @return 参数数据集
     */
    @Override
    public List<Params> selectParamsList(Params params) {
        return paramsMapper.selectParamsList(params);
    }

    /**
     * 新增参数数据集
     *
     * @param params 参数数据集
     * @return 结果
     */
    @Override
    public int insertParams(Params params) {
        params.setCreateTime(DateUtils.getNowDate());
        int i = paramsMapper.insertParams(params);
        int maxSort = this.interfaceParamsFieldRelationMapper.getMaxSort(params.getType(), params.getInterfaceId(), params.getIsInParam()) + 1;
        InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
        interfaceParamsFieldRelation.setInterfaceId(params.getInterfaceId());
        interfaceParamsFieldRelation.setObjectType("2");
        interfaceParamsFieldRelation.setType(params.getType());
        interfaceParamsFieldRelation.setObjectId(params.getId());
        interfaceParamsFieldRelation.setSortNum(maxSort);
        interfaceParamsFieldRelation.setIsInParam(params.getIsInParam());
        this.interfaceParamsFieldRelationMapper.insertInterfaceParamsFieldRelation(interfaceParamsFieldRelation);
        return i;
    }

    /**
     * 修改参数数据集
     *
     * @param params 参数数据集
     * @return 结果
     */
    @Override
    public int updateParams(Params params) {
        params.setUpdateTime(DateUtils.getNowDate());
        return paramsMapper.updateParams(params);
    }

    /**
     * 批量删除参数数据集
     *
     * @param ids 需要删除的参数数据集主键
     * @return 结果
     */
    @Override
    public int deleteParamsByIds(String ids) {
        return paramsMapper.deleteParamsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除参数数据集信息
     *
     * @param id 参数数据集主键
     * @return 结果
     */
    @Override
    public int deleteParamsById(Long id) {
        return paramsMapper.deleteParamsById(id);
    }

    @Override
    public List<FieldInfo> selectFieldInfoList(List<Long> ids) {
        return paramsMapper.selectFieldInfoList(ids);
    }

    @Override
    public boolean check(Params params) {
        List<Params> list = paramsMapper.check(params);
        return CollectionUtil.isNotEmpty(list);
    }
}
